//
//  MainColorAppViewController.m
//  QKZhi-iOS
//
//  Created by CL on 2017/1/4.
//  Copyright © 2017年 cn.com.qktz. All rights reserved.
//

#import "MainColorAppViewController.h"
#import "Appearance.h"
#import "UINavigationBar+Awesome.h"
#import "UIColor+GNUtil.h"


@interface MainColorAppViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *bar;

@end

@implementation MainColorAppViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //导航处理相关
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//导航栏左右两边按钮颜色
    self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    self.navBarHairlineImageView.hidden = YES;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"==============%@",[self class]);
    //导航返回按钮
    if (self.navigationController.childViewControllers.count > 1) {
        [self setNavLeftItemWithTitle:nil img:@"返回" frame:CGRectMake(0, 0, 50, 21)];
    }
    
    if ([self isKindOfClass:NSClassFromString(@"InformationMainViewController")] ||
        [self isKindOfClass:NSClassFromString(@"EventMainViewController")] ||
        [self isKindOfClass:NSClassFromString(@"QuizMianViewController")] ||
        [self isKindOfClass:NSClassFromString(@"RecommendMainViewController")] ||
        [self isKindOfClass:NSClassFromString(@"MineMainViewController")]) {
    
    }
}

//拿到导航灰色的线条
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


-(UIButton *)leftBarButtonItem:(NSString *)normalImage btnTitile:(NSString *)Titile font:(CGFloat)sizeFont imagePorsion:(NSInteger )type{
    
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 17)];
    if (normalImage) {
        [left setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
        [left setImage:[UIImage imageNamed:normalImage] forState:UIControlStateHighlighted];
    }
    if (Titile) {
        [left setTitle:Titile forState:UIControlStateNormal];
        [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        left.titleLabel.font = [UIFont boldSystemFontOfSize:sizeFont];
    }
//    if (type ==1) {
//
//    }
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] init];
    rightBarButtonItem.customView = left;
    if ([normalImage isEqualToString:@"返回白色"]) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        negativeSpacer.width = - 10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,rightBarButtonItem];
    }else {
        self.navigationItem.leftBarButtonItem =rightBarButtonItem;
    }
    return left;
    
    
}


//右边文字、图片
-(UIButton *)rightBarButtonItem:(NSString *)normalImage btnTitile:(NSString *)Titile font:(CGFloat)sizeFont{
    UIButton *right = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 17)];
    if (normalImage) {
        [right setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
         [right setImage:[UIImage imageNamed:normalImage] forState:UIControlStateHighlighted];
    }
    if (Titile) {
        [right setTitle:Titile forState:UIControlStateNormal];
        [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        right.titleLabel.font = [UIFont boldSystemFontOfSize:sizeFont];
    }
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    return right;
}

//左边文字、图片
-(UIButton *)leftBarButtonItem:(NSString *)normalImage btnTitile:(NSString *)Titile font:(CGFloat)sizeFont{
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,17)];
    if (normalImage) {
        [left setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
        [left setImage:[UIImage imageNamed:normalImage] forState:UIControlStateHighlighted];
    }
    if (Titile) {
        [left setTitle:Titile forState:UIControlStateNormal];
        [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        left.titleLabel.font = [UIFont boldSystemFontOfSize:sizeFont];
    }
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] init];
    rightBarButtonItem.customView = left;
    if ([normalImage isEqualToString:@"返回白色"]) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        negativeSpacer.width = - 10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,rightBarButtonItem];
    }else {
        self.navigationItem.leftBarButtonItem =rightBarButtonItem;
    }
    return left;
}

//配置导航颜色和title颜色
- (void) setNavConfigStartColor:(NSString *)startColorStr endColor:(NSString *)endColorStr{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor colorWithHexString:startColorStr] endColor:[UIColor colorWithHexString:endColorStr]];
}


-(void)ApptitileView:(NSString *)titileName{
    if (titileName.length == 0) {
        return;
    }
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.0 - 100 / 2.0, 0, 100, 44)];
    titleLabel.text = titileName;
    titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
    titleLabel.textColor = [UIColor colorWithHexString:@"ffffff"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
}



/**
 * 左边导航按钮
 * @param title title和img和传nil，frame必须传
 */
-(void)setNavLeftItemWithTitle:(NSString *)title img:(NSString *)img frame:(CGRect)frame{
    UIButton *left = [[UIButton alloc] initWithFrame:frame];
    if (img) {
        [left setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
        [left setImage:[UIImage imageNamed:img] forState:UIControlStateHighlighted];
        [left setTitle:@"" forState:UIControlStateNormal];
        left.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);

    }
    if (title) {
        [left setTitle:title forState:UIControlStateNormal];
        [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        left.titleLabel.font = [UIFont systemFontOfSize:15];
        left.imageEdgeInsets = UIEdgeInsetsMake(0, - 10, 0, 0);
    }
    [left addTarget:self action:@selector(leftItemClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] init];
    rightBarButtonItem.customView = left;
    if ([img isEqualToString:@"返回白色"]) {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        negativeSpacer.width = - 10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,rightBarButtonItem];
    }else {
        self.navigationItem.leftBarButtonItem =rightBarButtonItem;
    }
    
}


/**
 * 导航返回按钮点击，pop出当前页面
 */
- (void)leftItemClicked {
    
    
    if (self.navigationController ) {
        if (self.backButtonPress) {
            self.backButtonPress();
        }else{
              [self.navigationController popViewControllerAnimated:YES];
        }
      
    }
}

#pragma mark 消息中心
/**
 * 左边导航消息中心
 */
-(void)setNavMessageLeftItemIsLeft:(BOOL)isLeft{
    UIButton *left = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 24)];
    [left setImage:[UIImage imageNamed:@"消息"] forState:UIControlStateNormal];
    [left setImage:[UIImage imageNamed:@"消息"] forState:UIControlStateHighlighted];
    [left setTitle:@"消息" forState:UIControlStateNormal];
    [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    left.titleLabel.font = [UIFont systemFontOfSize:15];
    [left addTarget:self action:@selector(messageItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    left.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, - 10);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(17, 2, 7, 7)];
    label.backgroundColor = [UIColor redColor];
    label.layer.cornerRadius = 3.5;
    label.tag = 999;
    label.layer.masksToBounds = YES;
    [left addSubview:label];
    


    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] init];
    leftBarButtonItem.customView = left;
    leftBarButtonItem.width = - 17;
    if (isLeft) {
      
        label.backgroundColor = [UIColor redColor];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }else {
        label.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
        self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    }
}

/**
 * 未读消息统计显示导航红点，需要的控制器调用
 */
- (void)refreshUnreadCountIsLeft:(BOOL)isLeft {
    NSArray *items = self.navigationItem.leftBarButtonItems;
    if (!isLeft) {
        items = self.navigationItem.rightBarButtonItems;
    }
   
}

/**
 * 消息中心导航按钮点击
 */
- (void)messageItemClicked:(UIButton *)sender {
  
}
- (UINavigationController *)loginNav
{
    if (!_loginNav) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        _loginNav = [story instantiateViewControllerWithIdentifier:@"LoginNav"];
    }
    return _loginNav;
}

- (UIViewController *)LoginController
{
    if (!_LoginController) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        _LoginController = [story instantiateViewControllerWithIdentifier:@"LoginController"];
    }
    return _LoginController;
}

@end
